﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace AttentenceCheckSystem
{
    class RecordItem
    {
        public RecordItem(DateTime dt)
        {
            RegularCheckInTime = new DateTime(dt.Year, dt.Month, dt.Day, 9, 0, 0);
            RegularCheckOutTime = new DateTime(dt.Year, dt.Month, dt.Day, 18, 0, 0);
            Late_minutes = 0;
            memo = MEMO.NONE;
            date = dt;
        }

        [TypeConverter(typeof(DateTimeTypeConverter))]
        public DateTime date
        {
            get;
            private set;
        }

        [TypeConverter(typeof(DateTimeTypeConverter))]
        public DateTime? CheckInTime
        {
            get;
            private set;

        }

        [TypeConverter(typeof(DateTimeTypeConverter))]
        public DateTime? CheckOutTime
        {
            get;
            private set;

        }

        [TypeConverter(typeof(DateTimeTypeConverter))]
        public DateTime RegularCheckInTime
        {
            get;
            private set;
        }

        [TypeConverter(typeof(DateTimeTypeConverter))]
        public DateTime RegularCheckOutTime
        {
            get;
            private set;
        }

        public bool IsMorningOff
        {
            get;
            private set;
        }

        public bool IsAfternoonOff
        {
            get;
            private set;
        }

        public bool IsHoliday
        {
            get; set;
        }

        public enum MEMO
        {
            MISS_BOTH,
            MISS_MORNING,
            MISS_AFTERNOON,
            LATE,
            LEAVE_EARLY,
            NONE
        }

        public MEMO memo
        {
            get;
            private set;
        }

        public string status
        {
            get{
                string str = "";
                switch (memo)
                {
                    case RecordItem.MEMO.MISS_BOTH:
                        str = "上下班均未检测到打卡记录";
                        break;
                    case RecordItem.MEMO.MISS_MORNING:
                        str = "上班未检测到打卡记录";
                        break;
                    case RecordItem.MEMO.MISS_AFTERNOON:
                        str = "下班未检测到打卡记录";
                        break;
                    case RecordItem.MEMO.LEAVE_EARLY:
                        str = "下班早退";
                        break;
                    case RecordItem.MEMO.LATE:
                        str = string.Format("上班迟到{0}分钟", Late_minutes);
                        break;
                }
                return str;
            }
        }

        public int Late_minutes
        {
            get;
            private set;
        }

        public void AddRecord(DateTime dt)
        {
            if (IsAfternoonOff)
            {
                if (6 <= dt.Hour && dt.Hour <= 12)
                {
                    // 上班刷卡
                    if (CheckInTime == null || dt < CheckInTime)
                        CheckInTime = dt;
                }
                else
                {
                    // 下班刷卡
                    if (CheckOutTime == null || dt > CheckOutTime)
                        CheckOutTime = dt;
                }
            }
            else if (IsMorningOff)
            {
                if (dt.Hour <= 16)
                {
                    // 上班刷卡
                    if (CheckInTime == null || dt < CheckInTime)
                        CheckInTime = dt;
                }
                else
                {
                    // 下班刷卡
                    if (CheckOutTime == null || dt > CheckOutTime)
                        CheckOutTime = dt;
                }
            }
            else
            {
                if (6 <= dt.Hour && dt.Hour <= 14)
                {
                    // 上班刷卡
                    if (CheckInTime == null || dt < CheckInTime)
                        CheckInTime = dt;
                }
                else
                {
                    // 下班刷卡
                    if (CheckOutTime == null || dt > CheckOutTime)
                        CheckOutTime = dt;
                }
            }
        }

        public void ApplyOff(bool morning, bool afternoon)
        {
            if (morning)
                IsMorningOff = true;

            if (afternoon)
                IsAfternoonOff = true;

            // 更改规定上班时间
            if (morning)
            {
                RegularCheckInTime = new DateTime(RegularCheckInTime.Year,
                                                  RegularCheckInTime.Month,
                                                  RegularCheckInTime.Day,
                                                  14, 0, 0);
            }
            if (afternoon && RegularCheckInTime.Hour == 14)
            {
                RegularCheckInTime = new DateTime(RegularCheckInTime.Year,
                                  RegularCheckInTime.Month,
                                  RegularCheckInTime.Day,
                                  18, 0, 0);
            }

            // 更改规定下班时间
            if (afternoon)
            {
                RegularCheckOutTime = new DateTime(RegularCheckOutTime.Year,
                                                   RegularCheckOutTime.Month,
                                                   RegularCheckOutTime.Day,
                                                   14, 0, 0);
            }
            if (morning && RegularCheckOutTime.Hour == 14)
            {
                RegularCheckOutTime = new DateTime(RegularCheckOutTime.Year,
                                   RegularCheckOutTime.Month,
                                   RegularCheckOutTime.Day,
                                   9, 0, 0);
            }

        }

        public void CheckOutVeryLate(DateTime time)
        {
            DateTime PreDay = date.AddDays(-1);
            DateTime CheckPoint1 = new DateTime(date.Year, date.Month, date.Day, 2, 0, 0);
            DateTime CheckPoint2 = date;
            DateTime CheckPoint3 = new DateTime(PreDay.Year, PreDay.Month, PreDay.Day, 22, 30, 0);
            if (time > CheckPoint1)
            {
                RegularCheckInTime = new DateTime(date.Year, date.Month, date.Day, 13, 0, 0);
            }
            else if (time > CheckPoint2)
            {
                RegularCheckInTime = new DateTime(date.Year, date.Month, date.Day, 10, 0, 0);
            }
            else if (time > CheckPoint3)
            {
                RegularCheckInTime = new DateTime(date.Year, date.Month, date.Day, 9, 30, 0);
            }
        }

        public void PostProcess(ref int nLate30)
        {
            if (IsHoliday)
                return;

            if (CheckInTime == null && CheckOutTime == null)
            {
                memo = MEMO.MISS_BOTH;
                return;
            }

            if (CheckInTime == null)
            {
                memo = MEMO.MISS_MORNING;
                return;
            }

            if (CheckOutTime == null)
            {
                memo = MEMO.MISS_AFTERNOON;
                return;
            }

            TimeSpan temp = new TimeSpan(CheckInTime.Value.Ticks - RegularCheckInTime.Ticks);
            TimeSpan temp1 = new TimeSpan(CheckOutTime.Value.Ticks - RegularCheckOutTime.Ticks);
            if (temp.TotalMinutes > 0)
            {
                double temp2 = temp.TotalMinutes < 30 ? temp.TotalMinutes : 30;
                if (temp2 < temp.TotalMinutes)
                {
                    memo = MEMO.LATE;
                   
                    if (temp.TotalMinutes > 30)
                    { 
                        // 迟到30分钟以上
                        if (nLate30 < 5)
                            Late_minutes = Convert.ToInt32(temp.TotalMinutes - temp2);
                        else
                            Late_minutes = Convert.ToInt32(temp.TotalMinutes);
                        nLate30++;
                    }
                    else
                        Late_minutes = Convert.ToInt32(temp.TotalMinutes - temp2);
                    return;
                }
            }

            if (temp1.Minutes < 0)
            {
                memo = MEMO.LEAVE_EARLY;
            }

        }
    }
}
